/**
 * 将参数格式化后拼接在url上
 *
 * 值为对象则自动用JSON.stringify处理
 * 值为undefined、null与空字符串时移除
 * @param {路径, String} url
 * @param {参数, Object} params
 */
const getUrlWithParams = (
  url: string,
  params: Record<string, string | number | boolean | null>
) => {
  if (!params || JSON.stringify(params) === '{}') {
    return url;
  }
  const list: string[] = [];
  Object.keys(params).forEach((key: string) => {
    let item = params[key];
    if (typeof item === 'object') {
      item = JSON.stringify(item);
    }
    if (item !== undefined && item !== 'null' && item !== '') {
      list.push(`${key}=${item}`);
    }
  });
  return list.length > 0 ? `${url}?${list.join('&')}` : url;
};
export default getUrlWithParams;
